import { login } from '@/api/user'
import { getToken, setToken, removeToken, timeStampToken } from '@/utils/auth'

const state = {
  token: getToken()
}

const mutations = {
  StoreUser(state, payload) {
    // 储存token
    state.token = payload
    setToken(payload)
  },
  // 删
  logeRemov(state, payload) {
    state.token = null
    removeToken()
  }
}

const actions = {
  // 登录
  async login(context, payload) {
    try {
      const res = await login(payload)
      console.log(res)
      context.commit('StoreUser', res.token)
      timeStampToken()
    } catch (err) {
      console.log(err)
      console.dir(err)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

